package com.bjb.vr.common.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bjb.vr.common.dto.ExecutedSql;
import com.bjb.vr.common.dto.SyncData;
import com.bjb.vr.common.mapper.SyncDataMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author admin
 * @description 针对表【sync_data(这个表用到云端服务，用于保存变更的SQL语句。本地服务不需要使用，此表也不会进行同步)】的数据库操作Service实现
 * @createDate 2022-07-26 13:18:12
 */
@Slf4j
@Service
public class SyncDataService extends ServiceImpl<SyncDataMapper, SyncData> {

    @Resource
    private SyncDataMapper syncDataMapper;

    @Resource
    private ExecutedSqlService executedSqlService;

    /**
     * 保存同步数据
     *
     * @param syncData
     */
    public void saveData(SyncData syncData) {
        try {
            syncDataMapper.insert(syncData);
            ExecutedSql executedSql = new ExecutedSql();
            BeanUtils.copyProperties(syncData, executedSql);
            executedSqlService.saveDataToExecute(executedSql);
        } catch (Exception e) {
            log.error("数据执行异常--> 表名: {} 操作: {} 数据ID: {} 错误信息: {}", syncData.getTableName(), syncData.getEventType(), syncData.getDataId(), e.getCause().getMessage());
        }
    }
}




